package com.example.administrator.youlu.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.v4.content.ContextCompat;

import com.example.administrator.youlu.been.Contact;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class BitmapUtils {

    /**
     * @param context
     * @param resid
     * @return 本地资源转换成bitmap
     */
    public static Bitmap getDrawBleOrBitmap(Context context, int resid){


        Bitmap bitmap=null;
        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP){
            Drawable vectorDrawable = context.getDrawable(resid);
            bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
                    vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            vectorDrawable.draw(canvas);
            return bitmap;


        }else {
            bitmap = BitmapFactory.decodeResource(context.getResources(), resid);
        }
        return bitmap;
    }

    public static Bitmap getDrawBleOrBitmap2(Context context, int resid){


        Bitmap bitmap=null;
        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP){
            BitmapDrawable r = (BitmapDrawable) ContextCompat.getDrawable(context,resid);
            Bitmap bmp =  r.getBitmap();
            return bmp;


        }else {
            bitmap = BitmapFactory.decodeResource(context.getResources(), resid);
        }
        return bitmap;
    }

    /**
     * @param url
     * @return File转Bitmap
     * @throws FileNotFoundException
     */
    public static Bitmap getFileOrBitmap(String url) throws FileNotFoundException {
        FileInputStream fis = new FileInputStream(url);
        Bitmap bitmap  = BitmapFactory.decodeStream(fis);
        return bitmap;

    }
}
